package cn.the.angry.tests;

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

import org.jbox2d.collision.AABB;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.World;

import cn.the.angry.model.Bird;
import cn.the.angry.model.Ground;
import cn.the.angry.model.Pig;
import cn.the.angry.model.RectBaseElement;
import cn.the.angry.resourcemanager.ResourceManager;
import cn.the.angry.util.AngryPigDebugDraw;

public class G2Test extends AbstractTest {
	
	private int frames = 0;
	private long totalTime = 0;  
    private long curTime = System.currentTimeMillis();  
    private long lastTime = curTime;
    private int fps; 
	
	
	private final AABB worldAABB;
	private final World mWorld;
	private final Bird bird1;
	private final Bird bird2;
//	private final Bird bird3;
//	private final Bird bird4;
//	private final Bird bird5;
//	private Bird bird6;
//	private Bird bird7;
//	private Bird bird8;
//	private Bird bird9;
//	private Bird bird10;
	private final Pig pig1;
	private final Pig pig2;
	private final Pig pig3;
	private final Pig pig4;
	private final Pig pig5;
	private final RectBaseElement wood1;
	private final RectBaseElement wood2;
	private final RectBaseElement wood3;
	private final int GROUND_HEIGHT=HEIGHT-200;
	
	private final int targetFPS = 40;
	
	private final AngryPigDebugDraw debugDraw;
	
	public G2Test(){
		worldAABB = new AABB();
		worldAABB.lowerBound.set(0, 0);
		worldAABB.upperBound.set(1000, 1000);
		
		debugDraw = new AngryPigDebugDraw(WIDTH, HEIGHT, 1.0f);
		
		mWorld = new World(worldAABB, new Vec2(0f, 50f), true);
		
		
		//mWorld.setDebugDraw(debugDraw);
		bird1 = new Bird();
		bird1.createBird(mWorld, 0, 0f, 2f, 0.8f, 0.2f);
		bird2 = new Bird();
		bird2.createBird(mWorld, 350f, 10f, 2f, 0.8f, 0.2f);
//		bird3 = new Bird();
//		bird3.createBird(mWorld, 90f, 10f, 2f, 0.8f, 0.2f, 42f);
//		bird4 = new Bird();
//		bird4.createBird(mWorld, 130f, 10f, 2f, 0.8f, 0.2f, 42f);
//		bird5 = new Bird();
//		bird5.createBird(mWorld, 170f, 10f, 2f, 0.8f, 0.2f, 42f);
//		bird6 = new Bird();
//		bird6.createBird(mWorld, 210f, 10f, 2f, 0.8f, 0.2f, 42f);
//		bird7 = new Bird();
//		bird7.createBird(mWorld, 250f, 10f, 2f, 0.8f, 0.2f, 42f);
//		bird8 = new Bird();
//		bird8.createBird(mWorld, 290f, 10f, 2f, 0.8f, 0.2f, 42f);
//		bird9 = new Bird();
//		bird9.createBird(mWorld, 330f, 10f, 2f, 0.8f, 0.2f, 42f);
//		bird10 = new Bird();
//		bird10.createBird(mWorld, 370f, 10f, 2f, 0.8f, 0.2f, 42f);
		
		pig1 = new Pig();
		pig1.createPig(mWorld, 410f, 10f, 5f, 0.8f, 0.2f, 50f);
		pig2 = new Pig();
		pig2.createPig(mWorld, 450f, 10f, 5f, 0.8f, 0.2f, 50f);
		pig3 = new Pig();
		pig3.createPig(mWorld, 510f, 10f, 5f, 0.8f, 0.2f, 50f);
		pig4 = new Pig();
		pig4.createPig(mWorld, 550f, 10f, 5f, 0.8f, 0.2f, 50f);
		pig5 = new Pig();
		pig5.createPig(mWorld, 700f, 10f, 5f, 0.8f, 0.2f, 50f);
		
		new Ground(mWorld, 0, GROUND_HEIGHT, WIDTH, HEIGHT);
		
		wood1 = new RectBaseElement(ResourceManager.getLargeWoodRectangleImages());
		wood1.create(mWorld, 500f, 20f);
		wood1.getBody().setAngularVelocity((float) Math.PI);
		
		wood2 = new RectBaseElement(ResourceManager.getMiddleWoodBattenImages());
		wood2.create(mWorld, 550f, 20f);
		wood2.getBody().setAngularVelocity((float) Math.PI);
		
		wood3 = new RectBaseElement(ResourceManager.getMiddleWoodSquareImages());
		wood3.create(mWorld, 600f, 20f);
		wood3.getBody().setAngularVelocity((float) Math.PI);
		
		add(panel);
	}
	
	private final JPanel panel = new JPanel(){
		//private BufferedImage bufferImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		private Graphics2D g2;// = bufferImage.createGraphics();
		
		
		@Override
		public void update(Graphics g){
			paint(g);
		}
		
		@Override
		public void paint(Graphics g){
			g2 = (Graphics2D)g;
			g2.drawImage(backgroundBufferImage, 0, 0, null);
			debugDraw.setGraphics(g2);
			mWorld.step(1.0f/60f, 10);
			
			bird1.draw(g2);
			bird2.draw(g2);
//			bird3.draw(g2);
//			bird4.draw(g2);
//			bird5.draw(g2);Actor
//			bird6.draw(g2);
//			bird7.draw(g2);
//			bird8.draw(g2);
//			bird9.draw(g2);
//			bird10.draw(g2);
			
			pig1.draw(g2);
			pig2.draw(g2);
			pig3.draw(g2);
			pig4.draw(g2);
			pig5.draw(g2);
			wood1.draw(g2);
			wood2.draw(g2);
			wood3.draw(g2);
			
			
			g2.drawString(("FPS is "+fps).intern(), 15, 15);
			//caculate time
			lastTime = curTime;  
            curTime = System.currentTimeMillis();  
            totalTime += curTime - lastTime;  
            if (totalTime > 1000) {  
                totalTime -= 1000;  
                fps = frames;  
                frames = 0;  
            }  
            ++frames;
            
            g2.drawImage(foregroundBufferImage, 0, 0, null);
            
            g2.dispose();
		}
	};
	
	private final Runnable update = new Runnable(){
		@Override
		public void run(){
			long cTime = System.nanoTime();
			long lastTime = cTime;
			long count = 0;
			
			while (true) {
				count = System.nanoTime();
				
				panel.repaint();
			}
		}
	};
	
	public void startTest(){
		new Thread(update).start();
	}
	
	public static void main(String[] args)
    {
        final G2Test main = new G2Test();
        main.setVisible(true);
        new Thread(){
        	@Override
        	public void run(){
        		try {
        			Thread.sleep(1000);
				} catch (Exception e) {
				}
				main.startTest();
        	}
        }.start();
    }
}
